from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import uvicorn
import os

# 导入API路由
from api import api_router

# 导入页面路由
from api.routes import router as page_router

# 导入启动事件处理
from api.startup import startup_event

# 创建应用实例
app = FastAPI(title="LiberTool - 个人工具箱", description="高效工具集成平台")

# 注册API路由
app.include_router(api_router)

# 注册页面路由
app.include_router(page_router)

# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")

# 挂载node_modules目录
app.mount("/node_modules", StaticFiles(directory="node_modules"), name="node_modules")

# 添加favicon路由
@app.get('/favicon.ico', include_in_schema=False)
async def favicon():
    return FileResponse('static/img/favicon.ico')

# 确保目录存在
os.makedirs("static/css", exist_ok=True)
os.makedirs("static/js", exist_ok=True)
os.makedirs("static/img", exist_ok=True)
os.makedirs("static/md", exist_ok=True)
os.makedirs("templates", exist_ok=True)
# 确保日志目录存在
os.makedirs("logs", exist_ok=True)

# 注册启动事件
@app.on_event("startup")
async def on_startup():
    await startup_event()

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=9091, reload=True)